home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1833 / 1833.xpi / chrome / yoono.jar / content / yoono / bookmarks / bookmarksxbel.js < prev    next >
Text File  |  2009-12-16  |  9KB  |  307 lines

  1. /*
  2.  * bookmarksxbel.js
  3.  *
  4.  * @author    david marteau <marteau.david@free.fr>
  5.  * @copyright 2005-2006 Yoono
  6.  */
  7.  
  8.  function ynXBELIOService() 
  9.  {
  10.    this._bmUtils = new ynBookmarksUtils();
  11.    this._yns     = new Namespace(YOONO_NS);
  12.    this._owner   = "firefox-yoono-extension";
  13.  
  14.    this._allowDuplicateURL = this._bmUtils.getPref("extensions.yoono.import.allowDuplicateURL",false);
  15.  }
  16.  
  17.  ynXBELIOService.prototype =
  18.  {
  19.   /*
  20.    * import xbel xmlObject in  'root' folder
  21.    */
  22.    _import : function( xbel, root )
  23.    {
  24.      var foldersets=[[xbel,root]];
  25.      var folderset=null;
  26.  
  27.      var bmUtils   = this._bmUtils;
  28.      var yns       = this._yns;
  29.      var owner     = this._owner;
  30.      var duplicate = this._allowDuplicateURL;
  31.  
  32.      const urlRes  = bmUtils.getArc("URL");
  33.      const nameRes = bmUtils.getArc("Name");
  34.  
  35.      while(foldersets.length>0)
  36.      {
  37.        folderset=foldersets.shift();
  38.     
  39.        var folder = folderset[0];
  40.        var parent = folderset[1];
  41.  
  42.        var parentContainer = bmUtils.getContainer(parent);
  43.        var numItems = 0; 
  44.              
  45.        for each (var element in folder.*) 
  46.        {
  47.          var res = null;
  48.          
  49.          try {
  50.            switch(element.localName().toUpperCase())
  51.            {
  52.               case "BOOKMARK": {   
  53.                 if(!duplicate) 
  54.                 {
  55.                   if(bmUtils.isBookmarked(urlRes,element.@href.toString()))
  56.                      break;
  57.                 }
  58.  
  59.                 var href = element.@href.toString();
  60.                 var info = element..metadata.(@owner == owner);
  61.                 
  62.                 // Check for live bookmarks
  63.                 if( info && info.@yns::livemark.toString() == "true" )
  64.                 {
  65.                   var feedurl = info.@yns::feedurl.toString();
  66.                   res = bmUtils.createLivemark(
  67.                          element.title.toString(),href,feedurl,
  68.                          element.desc.toString(),
  69.                          null); 
  70.                 }
  71.                 else 
  72.                 {
  73.                   var shortcutUrl = info ? info.@yns::keywords.toString() : ""; 
  74.                   res = bmUtils.createBookmark(
  75.                          element.title.toString(),href,shortcutUrl,
  76.                          element.desc.toString(),
  77.                          null); 
  78.                 }                
  79.                 numItems++;
  80.               }
  81.               break;          
  82.  
  83.               case "FOLDER": {
  84.                 // Check for toolbarfolder
  85.                 var info = element..metadata.(@owner == owner);
  86.                 if(info && info.@yns::toolbarfolder.toString() == "true") 
  87.                 {
  88.                   foldersets.push([element,bmUtils.getToolBarFolder()]);
  89.                 } 
  90.                 else 
  91.                 {
  92.                   var title = element.title.toString();
  93.                   
  94.                   if(!duplicate) {
  95.                      var foundRes = bmUtils.getBookmarkedResource(nameRes,title);
  96.                      if(foundRes) {
  97.                         foldersets.push([element,foundRes]);
  98.                         break;
  99.                      }
  100.                   }
  101.  
  102.                   var description = element.desc.toString();
  103.                   if(description) {
  104.                      /* XXX Need to set parent for setting description */
  105.                      res = bmUtils.createFolder(title,description,parent);
  106.                      foldersets.push([element,res]); 
  107.                   } else {
  108.                      res = bmUtils.createFolder(title,null,null);
  109.                      foldersets.push([element,res]);
  110.                   }                  
  111.                   numItems++;
  112.                 }
  113.               }
  114.               break;
  115.               
  116.               case "SEPARATOR":
  117.                 // Add seprator only if items have been inserted
  118.                 // before. This prevent useless separators to be
  119.                 // displayed.
  120.                 if(numItems>0)
  121.                    res = bmUtils.createSeparator();
  122.                 break;             
  123.            }
  124.  
  125.            if(res) {
  126.               var index = parentContainer.IndexOf(res);
  127.               if(index == -1)
  128.                  parentContainer.AppendElement(res);
  129.            }
  130.          } 
  131.          catch(exc)
  132.          {
  133.            // bmUtils.error("yoono:xbel",exc);
  134.            throw(exc);
  135.          }
  136.        }
  137.      }
  138.    },
  139.  
  140.   /*
  141.    * export 'root'  folder to xbel xmlObject
  142.    */
  143.    _export : function( xbel, root )
  144.    {
  145.      var bmUtils = this._bmUtils;
  146.      var yns     = this._yns;
  147.      var owner   = this._owner;
  148.      
  149.      const RDF  = bmUtils.RDF;
  150.      const RDFC = bmUtils.RDFC; 
  151.  
  152.      const nsIRDFResource = Components.interfaces.nsIRDFResource;
  153.  
  154.      const urlRes     = bmUtils.getArc("URL");
  155.      const nameRes    = bmUtils.getArc("Name");
  156.      const descRes    = bmUtils.getArc("Description");
  157.      const keywordRes = bmUtils.getArc("ShortcutURL");
  158.      const iconRes    = bmUtils.getArc("Icon");
  159.      const feedRes    = bmUtils.getArc("FeedURL");
  160.      const dateRes    = bmUtils.getArc("BookmarkAddDate");
  161.  
  162.      var ds = bmUtils.BKMS;  
  163.  
  164.      var foldersets = [[xbel,root]];
  165.      var folderset  = null;  
  166.           
  167.      var nodetree = [];
  168.  
  169.      while(foldersets.length>0)
  170.      {
  171.        folderset = foldersets.shift();
  172.  
  173.        var folder = folderset[0];
  174.        var parent = folderset[1];
  175.  
  176.        var parentContainer = bmUtils.getContainer(parent);
  177.        var elements        = parentContainer.GetElements();
  178.        
  179.        var subtree = [];
  180.        
  181.        while (elements.hasMoreElements()) 
  182.        {
  183.          var res = elements.getNext().QueryInterface(nsIRDFResource);
  184.  
  185.          var separator = false;
  186.          var isFolder  = false;
  187.          var element   = null;
  188.          var info      = null;
  189.  
  190.          var type = bmUtils.getType(res);
  191.  
  192.          switch(type)
  193.          {
  194.            case "Bookmark": {
  195.                 var url      = bmUtils.getProperty(res,urlRes);              
  196.                 var keywords = bmUtils.getProperty(res,keywordRes);  
  197.                 element = <bookmark href={url}/>;
  198.                 if(keywords) {
  199.                    info = <metadata owner={owner} xmlns:yns={YOONO_NS}
  200.                                     yns:keywords={keywords}/>;
  201.                 }
  202.               }
  203.               break;
  204.  
  205.            case "Livemark": {
  206.                 var url      = bmUtils.getProperty(res,urlRes);              
  207.                 var feedurl  = bmUtils.getProperty(res,feedRes);
  208.                 element = <bookmark href={url}/>;
  209.                 info    = <metadata owner={owner} xmlns:yns={YOONO_NS}
  210.                                     yns:livemark="true" 
  211.                                     yns:feedurl={feedurl}/>;
  212.               }
  213.               break;
  214.  
  215.            case "Folder": 
  216.               element = <folder/>;
  217.               foldersets.push([element,res]);
  218.               break;
  219.             
  220.            case "ToolbarFolder":
  221.               element = <folder/>;
  222.               info    = <metadata owner={owner} xmlns:yns={YOONO_NS} 
  223.                                   yns:toolbarfolder="true"/>;              
  224.               foldersets.push([element,res]);
  225.               break;
  226.  
  227.            case "BookmarkSeparator":
  228.               element   = <separator/>;
  229.               separator = true;
  230.               break;
  231.          }
  232.  
  233.          if(element) 
  234.          {           
  235.            if(!separator) 
  236.            {
  237.              var name = bmUtils.getProperty(res,nameRes);
  238.              var desc = bmUtils.getProperty(res,descRes);
  239.  
  240.              if(name) element.* += <title>{name}</title>;
  241.              if(desc) element.* += <desc>{desc}</desc>;  
  242.              if(info) element.* += <info>{info}</info>;
  243.            }
  244.            
  245.            subtree.push([element,folder]); 
  246.          }
  247.        }
  248.  
  249.        nodetree = nodetree.concat(subtree.reverse()); 
  250.      }
  251.  
  252.      while(nodetree.length>0)
  253.      {
  254.        var node = nodetree.pop();
  255.        node[1].* += node[0];
  256.      }
  257.  
  258.      return xbel;
  259.    },
  260.  
  261.    /*
  262.     * Export bookmarks to XBEL format
  263.     */
  264.    exportBookmarks : function () 
  265.    {        
  266.      var bmUtils = this._bmUtils;
  267.  
  268.      var xbel   = <xbel version="1.0" xmlns:yns={YOONO_NS}/>;  
  269.      var bmRoot = bmUtils.getRoot();
  270.   
  271.      var title = bmUtils.getPropertyByName(bmRoot,"Name");
  272.      var desc  = bmUtils.getPropertyByName(bmRoot,"Description");
  273.   
  274.      if(title) xbel.* += <title>{title}</title>;
  275.      if(desc)  xbel.* += <desc>{desc}</desc>;
  276.      
  277.      xbel = this._export(xbel,bmRoot);
  278.      return xbel;
  279.    }, 
  280.  
  281.    /*
  282.     * Import bookmarks from XBEL format
  283.     */
  284.    importBookmarks : function ( xbel, aBookmarkFolder ) 
  285.    {
  286.      if(!aBookmarkFolder)
  287.          aBookmarkFolder = this._bmUtils.getRoot();     
  288.           
  289.      this._bmUtils.beginUpdateBatch(); 
  290.    
  291.      var except;
  292.      
  293.      try {
  294.        this._import(xbel,aBookmarkFolder);
  295.      } catch(ex) {
  296.        except = ex;
  297.      }
  298.      
  299.      this._bmUtils.flush();
  300.      this._bmUtils.endUpdateBatch();
  301.      
  302.      if(except)
  303.         throw except;
  304.    }
  305.  };
  306.  
  307.